require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIArenaSpecialBattle = {};
HelperFunc.SetUIEnv(UIArenaSpecialBattle)
local this = UIArenaSpecialBattle;

this.hasInit = false;

this.btnBackButton = nil;
this.listItemGrid = nil;
this.backFunction = nil;
this.specialData = {};
this.destroyed = false;
this.nothingController = nil;

function UIArenaSpecialBattle.Init(cb, _arena_type) -- _arena_type 竞技场类型
    --todo 应该小写 暂时先大写
    backFunction = cb;
    this.cur_arena_type = _arena_type or EnumConst.ArenaBattleType.Normal
    local name, packageName = getUIName(PanelResNames.UIArenaSpecialBattle);
    panelMgr:Push(name, packageName, this.onCreate, Config.useLocalUI);
end

function UIArenaSpecialBattle.onCreate(obj)
    this.destroyed = false;
    if not hasInit then
        hasInit = true;
        btnBackButton = obj:GetChild("BackButton");
        bgClose = obj:GetChild("bgClose");
        listItemGrid = obj:GetChild("itemGrid");
        nothingController = obj:GetController("nothing");

        this.addEvent();
    end

    if this.cur_arena_type == EnumConst.ArenaBattleType.Normal then
        logicMgr.ArenaManager.requestSharedRecords()
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.Cross then
        logicMgr.ArenaManager.requestCrossSharedRecords()
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        logicMgr.ArenaManager.requestCrossTeamSharedRecords()
    else
        error("错误竞技场类型")
    end
    this.LocalListenEvent()
    this.refresh();
end

function UIArenaSpecialBattle.addEvent()
    btnBackButton.onClick:Add(this.onClickClose)
    bgClose.onClick:Add(this.onClickClose)
end

function UIArenaSpecialBattle.removeEvent()
    btnBackButton.onClick:Remove(this.onClickClose)
    bgClose.onClick:Remove(this.onClickClose)
end

function UIArenaSpecialBattle.LocalListenEvent()
    if this.cur_arena_type == EnumConst.ArenaBattleType.Normal then
        ListenEvent(Config.EventType.Fresh_ArenaSharedRecords, this.refresh)
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.Cross then
        ListenEvent(Config.EventType.Fresh_CrossArenaSharedRecords, this.refresh)
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        ListenEvent(Config.EventType.Fresh_CrossTeamArenaSharedRecords, this.refresh)
    else
        error("错误竞技场类型")
    end 
end

function UIArenaSpecialBattle.LocalCloseEvent() 
    if this.cur_arena_type == EnumConst.ArenaBattleType.Normal then
        CloseEvent(Config.EventType.Fresh_ArenaSharedRecords, this.refresh)
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.Cross then
        CloseEvent(Config.EventType.Fresh_CrossArenaSharedRecords, this.refresh)
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        CloseEvent(Config.EventType.Fresh_CrossTeamArenaSharedRecords, this.refresh)
    else
        error("错误竞技场类型")
    end
end

function UIArenaSpecialBattle.OnDestroy()
    this.removeEvent();
    this.destroyed = true;
end

function UIArenaSpecialBattle.OnEnable()
    -- this.LocalListenEvent();
end

function UIArenaSpecialBattle.OnDisable()
    this.LocalCloseEvent();
end

function UIArenaSpecialBattle.refresh()
    local specialInfos = nil
    if this.cur_arena_type == EnumConst.ArenaBattleType.Normal then
        specialInfos = logicMgr.ArenaManager.getSharedRecords()
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.Cross then
        specialInfos = logicMgr.ArenaManager.getCrossSharedRecords()
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        specialInfos = logicMgr.ArenaManager.getCrossTeamSharedRecords()
    else
        error("错误竞技场类型")
    end
    --- @type table<number, BattleRecord> 战斗记录
    specialData = specialInfos;
    listItemGrid.itemRenderer = this.setData;
    listItemGrid.numItems = #specialData;
    if  #specialData <= 0 then
        this.nothingController.selectedIndex = 1;
    else
        this.nothingController.selectedIndex = 0;
    end
end

function UIArenaSpecialBattle.setData(index, obj)
    local itemCtr = CtrlManager.GetCtrl(PanelNames.ArenaSpecialBattleItem);
    local data = specialData[index + 1];
    itemCtr.initData(obj, data);

    local btnShare = obj:GetChild("btnShare");
    local btnPlay = obj:GetChild("btnPlay");

    btnShare.onClick:Clear();
    btnShare.onClick:Add(function()
        this.onClickItemShare(data);
    end)
    btnPlay.onClick:Clear();
    btnPlay.onClick:Add(function()
        this.onClickItemPlay(data);
    end)
end

function UIArenaSpecialBattle.onClickItemShare(data)
    log("UIArenaSpecialBattle.onClickItemShare");
end

--- @param data BattleRecord 分享战斗记录
function UIArenaSpecialBattle.onClickItemPlay(data)
    --- 请求战斗命令
    BattlePlaybackManager.setBattleResult(data.win);
    BattlePlaybackManager.setBattleRecord(data);

    BattlePlaybackManager.requestFightCmdThenPlay(data, EnumConst.FightTypeConst.ARENA, nil, function()
        showUI(PanelNames.UIArenaMain, this.cur_arena_type)
    end, function()
        return this.destroyed
    end);
end

function UIArenaSpecialBattle.onClickClose()
    closeUI(PanelNames.UIArenaSpecialBattle);
    if backFunction ~= nil then
        backFunction();
    end
end

return UIArenaSpecialBattle;
